<template>
    <div class="login_container">
        <el-row>
            <el-col :span="9" :xs="0"></el-col>
            <el-col :span="15" :xs="24">

                <el-form label-width="80px" class="login_form">
                    <h1>欢迎登录</h1>
                    <el-form-item label="用户名">
                        <el-input placeholder="请输入用户名" v-model="UserData.username"></el-input>
                    </el-form-item>
                    <el-form-item label="密码">
                        <el-input type="password" placeholder="用户密码" v-model="UserData.password"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <a href="">去注册</a>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" size="default" @click="goreqLogin" class="login_btn">登录</el-button>
                    </el-form-item>

                </el-form>

            </el-col>
        </el-row>
    </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue';
import { ElNotification } from 'element-plus';
import { useRouter, useRoute } from 'vue-router';
//引入用户相关的小仓库
import useUserStore from "@/store/modules/user";
import io from 'socket.io-client';

let socket: any = null;
let useStore = useUserStore();
const router = useRouter();
const route = useRoute()
//收集账号与密码的数据
let UserData = reactive({ username: '', password: '' });
// 登录按钮触发的函数
const goreqLogin = async () => {
    try {
        //保证登录成功
        await useStore.userLogin(UserData);
        // 获取全部好友
        await useStore.getfriend(useStore.userId);
        // 剔除其他信息，只保留所有好友id useStore.friendsList
        const friendIds = useStore.friendsList.map((friend:any) => friend._id);
        // 连接到 WebSocket 服务器
        socket = io('http://localhost:8080', {
            withCredentials: true
        });
        socket.emit('user login', useStore.userId, friendIds); // 将用户ID发送到服务器，通知服务器用户已登录, 将好友列表也传递给服务器
        // 接收消息
        socket.on('chat message', (data:any) => {
            const { sender, content } = data;
            console.log(`Received message from ${sender}: ${content}`);
        });
        //编程式导航跳转到展示数据首页
        //判断登录的时候,路由路径当中是否有query参数，如果有就往query参数挑战，没有跳转到首页
        let redirect: any = route.query.redirect;
        router.push({ path: redirect || '/' });
        //登录成功提示信息
        ElNotification({
            type: 'success',
            message: '欢迎回来',
        });
        // //登录成功加载效果也消失
        // loading.value = false;
    } catch (error) {
        // //登录失败加载效果消息
        // loading.value = false;
        //登录失败的提示信息
        ElNotification({
            type: 'error',
            message: (error as Error).message
        })
    }
};
</script>

<style scoped lang="scss">
.login_container {
    background: linear-gradient(to right, #89acec, #e9ebef);
    /* 渐变蓝色 */
    min-height: 100vh;
    /* 确保背景覆盖整个页面 */
    padding: 20px;

    .login_form {
        margin-top: 100px;
        padding: 20px 10px;
        background-color: #fff;
        border-radius: 20px;
        width: 100%;
        max-width: 500px;
        /* 设置最大宽度 */
        min-width: 320px;
        /* 设置最小宽度，适应较小的屏幕 */
        height: auto;
        /* 让高度自适应内容 */
        box-sizing: border-box;
        /* 确保内边距不会让宽度溢出 */

        h1 {
            text-align: center;
            font-size: 30px;
            color: #89acec;
            margin-bottom: 50px;
        }

        .login_btn {
            background-color: #89acec;
            margin-top: 10px;
            width: 100%;
        }

        /* 按钮悬停时样式 */
        .login_btn:hover {
            background-color: #76c7e0;
            /* 悬停时改变背景颜色 */
            transform: scale(1.02);
            /* 悬停时略微放大按钮 */
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            /* 添加阴影效果 */
        }
    }
}

/* 响应式设计: 当屏幕宽度小于 768px 时 */
@media (max-width: 768px) {
    .login_form {
        padding: 20px;
        margin-top: 20px;
        width: 90%;
    }

    h1 {
        font-size: 24px;
    }
}
</style>